package com.wsj.web.action.BmClassType;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wsj.service.IBmClassTypeServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.tool.StringUtil;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.BmClassType.BmClassTypeForm;
import com.wsj.web.subclass.function.ProductsParamentObj;
import com.wsj.web.vo.homeSite.BmClassType;
public class BmClassTypeAction extends DispatchAction {
	private IBmClassTypeServ bmClassTypeServImpl;
	
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BmClassTypeForm bmClassTypeForm=(BmClassTypeForm)form;
		BmClassType bmClassType=new BmClassType();
		
		BeanUtils.copyProperties(bmClassType, bmClassTypeForm);
		boolean b=this.bmClassTypeServImpl.add(bmClassType);
		if(b){
			request.setAttribute("add", "<script>alert('添加成功！！！');</script>");
		}
		
		return mapping.findForward("add");
		
	}
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
	      String[] idStr = request.getParameterValues("checkboxparam");
	      int[] intId= StringUtil.changeStrArrayToIntArray(idStr);
	      if(intId!=null){
	    	  for(int i=0;i<intId.length;i++){
	    			 this.bmClassTypeServImpl.delete(intId[i]);
	    			  request.setAttribute("del", "<script>alert('删除成功！！！');</script>");
	    		  } 
	    	  }
		return mapping.findForward("delete");
		
	}
	public ActionForward toupdate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BmClassTypeForm bmClassTypeForm=(BmClassTypeForm)form;
		BmClassType bmClassType=null;
		String strid=request.getParameter("id");
		
		if(strid!=null&&!strid.equals("")){
			bmClassType=this.bmClassTypeServImpl.getBmClassTypeById(Integer.valueOf(strid));
			BeanUtils.copyProperties(bmClassTypeForm, bmClassType);
			request.setAttribute("bmClassTypeForm", bmClassTypeForm);
		}
		
		
		return mapping.findForward("toupdate");
		
	}
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BmClassTypeForm bmClassTypeForm=(BmClassTypeForm)form;
		BmClassType bmClassType=null;
		if(bmClassTypeForm.getId()!=null&&bmClassTypeForm.getId()!=0){
			bmClassType=this.bmClassTypeServImpl.getBmClassTypeById(bmClassTypeForm.getId());
			
			bmClassType.setClassTypeName(bmClassTypeForm.getClassTypeName());
			bmClassType.setDel(bmClassTypeForm.getDel());
			
//			BeanUtils.copyProperties(bmClassType, bmClassTypeForm);
			boolean b=this.bmClassTypeServImpl.update(bmClassType);
			if(b){
				request.setAttribute("update", "<script>alert('修改成功！！！');</script>");
			}
		}
		
		
		
		return mapping.findForward("update");
		
	}
	
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String topage="list";
		this.loadBmClassTypeList(request);
		
		return mapping.findForward(topage);
		
		
	}
	
	
	private void loadBmClassTypeList(HttpServletRequest request){
		
		
	      ProductsParamentObj parament=new ProductsParamentObj();
	      
	      String[] ret=parament.dealWithParaments(request,
	    		  new String[]{PageContent.page,PageContent.pageSize,PageContent.doit,
	    		  PageContent.backPage,PageContent.nextPage});

	      
	      int page=PageContent.pageValue;
	      
	      int pageSize=PageContent.pageSizeValue;
	      
	      
	      
	      String doit="";
	      
	      //页码
	      if(ret[0]!=null)
	      {
	          page=Integer.parseInt(ret[0]);
	      }
	      
	      
	      //显示数
	      if(ret[1]!=null)
	      {
	          pageSize=Integer.parseInt(ret[1]);
	      }  
	      
	      
	      //跳转
	      if(ret[2]!=null)
	      {
	          doit=ret[2];
	      }
	      
	      //上一页
	      if(doit!=null&&doit.equals(PageContent.backPage)&&ret[3]!=null&&!ret[3].equals(""))
	      {
	          page=Integer.parseInt(ret[3]);
	      }
	      
	      //下一页
	      if(doit!=null&&doit.equals(PageContent.nextPage)&&ret[4]!=null&&!ret[4].equals(""))
	      {
	          page=Integer.parseInt(ret[4]);
	      }
	      
	      try {
				PageResultSet pageResultSet=this.bmClassTypeServImpl.loadBmClassTypeByParam(null,page, pageSize);
				  if(pageResultSet!=null)
					{
					  	// 设置上一页
						request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
						//下一页
						request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
						//当前页
						request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
						
						//总共页数
						request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
						//大小
						request.setAttribute(PageContent.pageSize,pageResultSet.getPageSize());
						

						//结果集
						List result=null;
						if(pageResultSet!=null)
						{
							result =pageResultSet.getResult();
						}
						
						else
						{
							result=new ArrayList();
						}
						
						request.setAttribute("BmClassTypeList", result);
						
					}
				  
				  
			} catch (BusinessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	

	public IBmClassTypeServ getBmClassTypeServImpl() {
		return bmClassTypeServImpl;
	}

	public void setBmClassTypeServImpl(IBmClassTypeServ bmClassTypeServImpl) {
		this.bmClassTypeServImpl = bmClassTypeServImpl;
	}

}
